package com.whfc.mach.manager.strategy.work;

import com.whfc.common.util.DateUtil;
import com.whfc.mach.dto.AppMachDaySegDTO;
import com.whfc.mach.enums.WorkState;
import com.whfc.mach.manager.util.MachSegmentUtil;
import com.whfc.mach.manager.util.MachUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
 * @Description: 船只的工时策略(停机 - > 怠机, 怠机 - > 工作)
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-03-17 15:23
 */
public class BoatWorkStrategy implements WorkStrategy {

    private Logger logger = LoggerFactory.getLogger(BoatWorkStrategy.class);

    /**
     * "08:00:00"
     */
    private static long START_TIMES = (8 * 60 * 60 + 0 * 60) * 1000;

    /**
     * 20:00:00
     */
    private static long END_TIMES = (20 * 60 * 60 + 0 * 60) * 1000;

    @Override
    public Integer amend(Integer workState) {
        Date now = new Date();
        Date dateBegin = DateUtil.getDateBegin(now);
        long diff = now.getTime() - dateBegin.getTime();
        if (diff >= START_TIMES && diff <= END_TIMES) {
            return WorkState.RUN.getValue();
        }
        return workState;
    }

    @Override
    public List<AppMachDaySegDTO> amend(List<AppMachDaySegDTO> segmentList) {


        for (AppMachDaySegDTO segDTO : segmentList) {
            // 怠机全部修改成工作
            if (MachUtil.isIdleState(segDTO.getWorkState())) {
                segDTO.setWorkState(WorkState.RUN.getValue());
            }
            // 停机全部修改成怠机
            else if (MachUtil.isStopState(segDTO.getWorkState())) {
                segDTO.setWorkState(WorkState.IDLE.getValue());
            }
        }
        // 合并相同分段
        List<AppMachDaySegDTO> amendSegList = MachSegmentUtil.mergeMachDaySegment(segmentList);
        logger.info("工作状态转换,{}->{}", segmentList.size(), amendSegList.size());
        return amendSegList;
    }
}
